﻿using System;
using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars;

namespace Com.OpenCode.Framework.DevForm
{
    public class StatusLabelUI : BarStaticItem, IStatusSetValue
    {
        #region 字段

        private BarItemLink _BarItemLink;

        #endregion

        private int _InformationIndex = -1;

        #region 构造函数

        public StatusLabelUI(BuildItemEventArgs args)
        {
            StatusBarServices.Register += StatusHelper_Register;
            args.Plugin.Owner = this;
            Disposed += StatusLabelUI_Disposed;
            Width = int.Parse(args.Plugin.Properties.Get("Width").ToString());
            _InformationIndex = int.Parse(args.Plugin.Properties.Get("InformationIndex").ToString());
            if (args.Plugin.Properties.Exist("Alignment"))
            {
                Alignment = GetAlignment(args.Plugin.Properties.Get("Alignment").ToString());
            }
            _BarItemLink = (args.Owner as StatusBarUI).ItemLinks.Add(this);
        }

        private BarItemLinkAlignment GetAlignment(string alignment)
        {
            BarItemLinkAlignment barItemLinkAlignment = BarItemLinkAlignment.Default;
            if (alignment.ToUpper() == "LEFT")
            {
                barItemLinkAlignment = BarItemLinkAlignment.Left;
            }
            else if (alignment.ToUpper() == "RIGHT")
            {
                barItemLinkAlignment = BarItemLinkAlignment.Right;
            }
            return barItemLinkAlignment;
        }

        private void StatusLabelUI_Disposed(object sender, EventArgs e)
        {
            StatusBarServices.Register -= StatusHelper_Register;
        }

        private void StatusHelper_Register(EditStatusValueArgs e)
        {
            if (e.Index == InformationIndex)
            {
                SetStatusValue(e.Statusvalue);
                SetStatusVisible(e.Visible);
            }
        }

        #endregion

        #region 事件方法

        #endregion

        #region 方法

        public void SetStatusValue(object value)
        {
            Caption = value.ToString();
        }

        public void SetStatusVisible(bool visible)
        {
            if (_BarItemLink != null)
            {
                _BarItemLink.Visible = visible;
            }
        }

        public int InformationIndex
        {
            get { return _InformationIndex; }
        }

        #endregion
    }
}